package com.jebel.framwork.common.util;

import io.vertx.core.json.JsonObject;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

public class SignUtil {

    public static String getSign(JsonObject param, String key) {
        List<String> params = new ArrayList<>();
        if (param != null && param.size() > 0) {
            Iterator iterator = param.iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                if (entry.getValue() != null && !entry.getValue().equals("") && !entry.getKey().toString().equals("sign")) {
                    params.add(entry.getValue().toString());
                }
            }
        }
        params.add(key);
        String[] paramArray = params.toArray(new String[0]);
        Arrays.sort(paramArray);
        StringBuilder sb = new StringBuilder();
        for (String s : paramArray) {
            if (s != null && !"null".equals(s)) {
                sb.append(s);
            }
        }
        System.out.println(sb.toString());
        return SHA.getSHA(sb.toString());
    }

    public static String getSign(JsonObject param) {
        List<String> params = new ArrayList<>();
        String label = param.getString("label");
        if (param != null && param.size() > 0) {
            Iterator iterator = param.iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                if (entry.getValue() != null && !entry.getValue().equals("") && !entry.getKey().toString().equals("sign")) {
                    params.add(entry.getValue().toString());
                }
            }
        }
        params.add(label);
        String[] paramArray = params.toArray(new String[0]);
        Arrays.sort(paramArray);
        StringBuilder sb = new StringBuilder();
        for (String s : paramArray) {
            if (s != null && !"null".equals(s)) {
                sb.append(s);
            }
        }
        return SHA.getSHA(sb.toString());
    }


    public static void main(String[] args) throws Exception {
        JsonObject params = new JsonObject();
        params.put("transactionID", String.valueOf(System.currentTimeMillis()));
        params.put("billId", "17097254571");
        params.put("simCardNo", "89860114841200255587");
        params.put("channelId", "HMT001");
        params.put("busiType", "21");
        params.put("custName", "测试");
        params.put("custCertNo", "413715198509217004");
        params.put("custCertAddr", "天津市滨海新区杭州道");
        params.put("gender", "1");
        params.put("nation", "汉");
        params.put("birthday", "1985-09-21");
        params.put("issuingAuthority", "天津市滨海新区公安局");
        params.put("certValiddate", "2018-10-15");
        params.put("certExpdate", "2038-10-15");
        params.put("accBilld", "17000");
        params.put("service", "agent.opencall");
        params.put("label", "HMT001");
        String encode = SignUtil.getSign(params, "pvWwmt4fKRrIdl84lvP8qzIIvNkJaaaaqfSWRlsGC15zoc3lq");
        System.out.println(params);
        System.out.println(encode);

        LocalDateTime localDateTime = LocalDateTime.now();
        int lastYear = localDateTime.getMonthValue() - 1 == 0 ? localDateTime.getYear() - 1 : localDateTime.getYear();
        int lastMonth = localDateTime.getMonthValue() - 1 == 0 ? 12 : localDateTime.getMonthValue() - 1;
        System.out.println(DateUtil.localDate2Date(LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), 1)));
        System.out.println(DateUtil.localDate2Date(LocalDate.of(lastYear, lastMonth, 1)));

        System.out.println("17090330055".substring(0, 7));

    }

}
